import java.util.Stack;

public class Sort {

    //直接插入排序

    /***
     * 时间复杂度：O(N^2)
     * 最好情况：O(N);
     * 元素集合越有序，直接插入排序算法的事件效率越高
     * 空间复杂度:O(1),是一种稳定的算法
     *
     * @param arr
     */

    public static void insertSort(int[] arr){
        for (int i = 1; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if(arr[j] > tmp){
                    arr[j + 1] = arr[j];
                }else {
                    arr[j + 1] = tmp;
                    break;
                }
            }
            arr[j+1] = tmp;
        }
    }
    public static void insertSort2(int[] arr){
        for (int i = 1; i < arr.length; i++) {
            int tmp = arr[i];
            for (int j = i - 1; j >= -1 ; j--) {
                if(j != -1 && arr[j] > tmp){
                    arr[j+1] = arr[j];
                }else {
                    arr[j+1] = tmp;
                    break;
                }
            }
        }
    }

    //希尔排序

    /***
     * 时间复杂度：n^1.3 - n^1.5
     * 希尔排序是对直接插入排序的一种优化，不稳定
     * @param arr
     */
    public static void shellSort(int[] arr){
        int gap = arr.length;
        while (gap > 1){
            gap /= 2;
            shell(arr,gap);
        }
    }

    private static void shell(int[] arr, int gap) {
        for (int i = gap; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - gap;
            for (; j >= 0; j-=gap) {
                if(arr[j] > tmp){
                    arr[j + gap] = arr[j];
                }else {
                    arr[j + gap] = tmp;
                    break;
                }
            }
            arr[j+gap] = tmp;
        }
    }
    //选择排序

    /***
     * 时间复杂度：O(N^2);
     * 空间复杂度：O(1);
     * 稳定性：不稳定
     * @param array
     */

    public static void selectSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if(array[j] < array[minIndex]){
                    minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }

    public static void selectSort2(int[] array){
        int left = 0;
        int right = array.length - 1;
        while(left < right){
            int minIndex = left;
            int maxIndex = left;
            for (int i = left + 1; i <= right; i++) {
                if(array[i] < array[minIndex]){
                    minIndex = i;
                }
                if(array[i] > array[maxIndex]){
                    maxIndex = i;
                }
            }
            swap(array,left,minIndex);
            if(left == maxIndex){
                maxIndex = minIndex;
            }
            swap(array,right,maxIndex);
            left++;
            right--;
        }
    }

    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    
    //堆排序

    /***
     * 时间复杂度:O(NlogN)
     * 空间复杂度:O(1);
     * 稳定性:不稳定;
     * @param array
     */
    public static void heapSort(int[] array){
        createHeap(array);
        int end = array.length - 1;
        while (end > 0){
            swap(array,0,end);
            shiftDown(array,0,end);
            end--;
        }
    }
    
    private static void createHeap(int[] array){
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0 ; parent--) {
            shiftDown(array,parent,array.length);
        }
    }

    /***
     *
     * @param array
     * @param parent 每棵子树的根节点
     * @param length 每棵子树调整的结束节点
     */
    private static void shiftDown(int[] array, int parent, int length) {
        int child = 2 * parent + 1;
        while(child < length){
            if(child + 1 < length && array[child] < array[child + 1]){
                child++;
            }
            if(array[child] > array[parent]){
                swap(array,child,parent);
                parent = child;
                child = parent * 2 + 1;
            }else {
                break;
            }
        }
    }

    //冒泡排序

    /***
     * 时间复杂度:(讨论的是没有优化状态的情况，也就是没有boolean 和 -i的元素) O(N^2)
     * 空间复杂度: O(1)
     * 稳定性: 稳定性
     * @param array
     */

    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length - 1; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if(array[j] > array[j+1]){
                    flag = true;
                    swap(array,j,j + 1);
                }
            }
            if(!flag){
                break;
            }
        }
    }

    //快速排序

    /***
     * 时间复杂度:1.最坏情况:当数组是有序的时候，是O(N^2)
     *           2.最好情况:O(NLogN);
     * 空间复杂度:1.最坏情况:O(N);
     *           2.最好情况下:O(logN)
     *  稳定性:不稳定
     * @param array
     */

    public static void quickSort(int[] array){
        quick(array,0,array.length - 1);
    }

    //快速排序的非递归实现
    private static void quickNor(int[] array, int start, int end){
        Stack<Integer> stack = new Stack<>();
        int pivot = partitionPotholing(array,start,end);
        if(pivot > start + 1){
            stack.push(start);
            stack.push(pivot - 1);
        }

        if(pivot < end - 1){
            stack.push(pivot + 1);
            stack.push(end);
        }

        while(!stack.isEmpty()){
            end = stack.pop();
            start = stack.pop();
            pivot = partitionPotholing(array,start,end);
            if(pivot > start + 1){
                stack.push(start);
                stack.push(pivot - 1);
            }

            if(pivot < end - 1){
                stack.push(pivot + 1);
                stack.push(end);
            }
        }
    }
    private static void quick(int[] array, int start, int end) {
        if(start >= end){
            return;
        }

        if(end - start + 1 <= 7){
            insertSortRange(array,start,end);
            return;
        }

        //优化1:三数取中法
        int midIndex = getMidIndex(array,start,end);
        swap(array,midIndex,start);
        int pivot = partitionPotholing(array,start,end);
        quick(array,start,pivot - 1);
        quick(array,pivot + 1,end);
    }

    private static void insertSortRange(int[] arr, int start, int end) {
        for (int i = start + 1; i <= end; i++) {
            int tmp = arr[i];
            int j = i - 1;
            for (; j >= start; j--) {
                if(arr[j] > tmp){
                    arr[j + 1] = arr[j];
                }else {
                    arr[j + 1] = tmp;
                    break;
                }
            }
            arr[j+1] = tmp;
        }
    }

    private static int getMidIndex(int[] array, int left, int right) {
        int midIndex = (left +  right) / 2;
        if(array[left] < array[right]){
            if(array[midIndex] < array[left]){
                return left;
            }else if(array[midIndex] > array[right]){
                return right;
            }else {
                return midIndex;
            }
        }else{
            if(array[midIndex] > array[left]){
                return left;
            }else if(array[midIndex] < array[right]){
                return right;
            }else{
                return midIndex;
            }
        }
    }


    //3.前后指针法
    private static int partitionFrontAndRearPointer(int[] array,int left,int right){
        int prev = left;
        int cur = prev + 1;
        while(cur <= right){
            if(array[cur] < array[left] && array[++prev] != array[cur]){
                swap(array,cur,prev);
            }
            cur++;
        }
        swap(array,left,prev);
        return prev;
    }



    //2.挖坑法(最重要)
    private static int partitionPotholing(int[] array,int left,int right){
        int standard = array[left];
        while(left < right){
            while(left < right && array[right] >= standard){
                right--;
            }
            array[left] = array[right];
            while(left < right && array[left] <= standard){
                left++;
            }
            array[right] = array[left];
        }
        array[left] = standard;
        return left;
    }
    
    

    //1.Hoare法
    private static int partitionHoare(int[] array, int left, int right) {
        int standard = array[left];
        int initialLeft = left;
        while(left < right){
            //为什么从后往前找而不是从前往后找
            //因为从后往前找的话相遇时对应的值一定小于standard;


            while (left < right && array[right] >= standard){
                //这里可以不要等于吗?
                //不可以,可能会出现死循环.
                right--;
            }
            while (left < right && array[left] <= standard){
                left++;
            }
            swap(array,left,right);
        }
        swap(array,left,initialLeft);
        return left;
    }

    //归并排序

    /***
     * 时间复杂度:O(NlogN)
     * 空间复杂度:O(N)
     * 稳定性:稳定
     * @param array
     */
    public static void mergeSort(int[] array){
        mergeSortTmp(array,0,array.length - 1);
    }

    //归并排序的非递归实现
    public static void mergeSortNor(int[] array){
        int gap = 1;
        while(gap < array.length){
            for (int i = 0; i < array.length; i = i + gap * 2) {
                int left = i;
                int mid = left + gap - 1;
                if(mid >= array.length){
                    mid = array.length - 1;
                }
                int right = mid + gap;
                if(right >= array.length){
                    right = array.length - 1;
                }
                merge(array,left,mid,right);
            }
            gap *= 2;
        }
    }

    private static void mergeSortTmp(int[] array,int left,int right){
        if(left >= right){
            return;
        }
        int mid = (left + right) / 2;
        mergeSortTmp(array,left,mid);
        mergeSortTmp(array,mid + 1,right);
        //全部分解完成 开始合并
        merge(array,left,mid,right);

    }

    private static void merge(int[] array,int left,int mid,int right){
        int[] tmp = new int[right - left + 1];
        int k = 0;
        int s1 = left;
        // int e1 = mid;
        int s2 = mid + 1;
        // int e2 = right;
        while(s1 <= mid && s2 <= right){
            if(array[s1] <= array[s2]){
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }

        while(s1 <= mid){
            tmp[k++] = array[s1++];
        }

        while(s2 <= right){
            tmp[k++] = array[s2++];
        }

         for(int i = 0;i < k;i++){
             array[i+left] = tmp[i];
         }
    }

    //计数排序

    /***
     * 时间复杂度:O(n+范围)
     * 空间复杂度:O(范围)
     * 稳定性:稳定
     * @param array
     */
    public static void countSort(int[] array){
        //找出最大值和最小值
        //O(n)
        int minVal = array[0];
        int maxVal = array[0];
        for(int i = 1;i < array.length;i++){
            if (array[i] < minVal) {
                minVal = array[i];
            }
            if (array[i] > maxVal){
                maxVal = array[i];
            }
        }

        //遍历原来的array数组,把每个元素放到计数数组中去,进行计数
        //O(n)
        int len = maxVal - minVal + 1;
        int[] count = new int[len];
        for(int i = 0;i < array.length;i++){
            int index = array[i];
            count[index - minVal]++;
        }

        //依次遍历count数组
        //O(范围)
        int index = 0;//放在外面
        for(int i = 0;i < count.length;i++){
            while(count[i] > 0){
                array[index] = i + minVal;
                index++;
                count[i]--;
            }
        }
    }




}
